package com.zp.demo.repush.service.impl;/**
 * 描述：
 *
 * @author: 张鹏
 * @Date: ${time} 2019/1/2
 */

import com.zp.demo.repush.service.defined.DefinedRePushFitter;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.types.Expiration;
import org.springframework.stereotype.Component;

/**
 * 说明：
 * @author 张鹏
 * @create
 */
@Log4j2
@Component
public class RePushFillterImpl implements DefinedRePushFitter {
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate ;
    @Override
    public boolean needRePush(String key, Integer time) {
        String newKey = "REPUSH_"+key;
        boolean flag = redisTemplate.execute(new RedisCallback<Boolean>() {
            @Override
            public Boolean doInRedis(RedisConnection redisConnection) throws DataAccessException {
                return redisConnection.set(newKey.getBytes(),"false".getBytes(),Expiration.milliseconds(time),RedisStringCommands.SetOption.SET_IF_ABSENT);
            }
        });
        if(!flag){
            log.debug("服务ID:"+key + "已经在重推队列，无需重推");
            return false;
        }
        return true;
    }

    @Override
    public boolean checkSuccessOnes(String key, Integer time) {
        String newKey = "CheckSuccess_" + key;
        boolean flag = redisTemplate.execute(new RedisCallback<Boolean>() {
            @Override
            public Boolean doInRedis(RedisConnection redisConnection) throws DataAccessException {
                return redisConnection.set(newKey.getBytes(), "true".getBytes(), Expiration.milliseconds(time), RedisStringCommands.SetOption.SET_IF_ABSENT);
            }
        });
        if (!flag) {
            log.debug("服务ID:" + key + "结果已经成功了，重复推送操作");
            return false;
        }
        return true;
    }

    @Override
    public boolean removeRePush(String key) {
        String newKey = "REPUSH_"+key;
        Long flag = redisTemplate.execute(new RedisCallback<Long>() {
            @Override
            public Long doInRedis(RedisConnection redisConnection) throws DataAccessException {
                return redisConnection.del(newKey.getBytes());
            }
        });
        return flag > 0 ? true:false;
    }

    @Override
    public boolean removeCheckSuccess(String key) {
        String newKey = "CheckSuccess_" + key;
        Long flag = redisTemplate.execute(new RedisCallback<Long>() {
            @Override
            public Long doInRedis(RedisConnection redisConnection) throws DataAccessException {
                return redisConnection.del(newKey.getBytes());
            }
        });
        return flag > 0 ? true:false;
    }
}
